package com.bjsxt.usersCenter.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.usersCenter.common.controller.BaseController;
import com.bjsxt.usersCenter.common.util.ConstatFinalUtil;
import com.bjsxt.usersCenter.common.util.EncryptUtil;
import com.bjsxt.usersCenter.users.pojo.AUsers;
import com.bjsxt.usersCenter.users.service.IUsersService;

@Controller
@RequestMapping("/head/users")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	@RequestMapping("/main")
	public String main()
	{
		return "/head/users/main";
	}
	@RequestMapping("/usersUpdate")
	public String usersUpdate(HttpServletRequest request)
	{
		HttpSession session=request.getSession();
		AUsers users=(AUsers) session.getAttribute("users");
	
		Map<String,Object> condMap=new HashMap<String, Object>();
		condMap.put("id", users.getId());
		condMap.put("extend", "true");
		users=this.usersService.findOneUsersService(condMap);
		
		request.setAttribute("users", users);

		return "/head/users/update";
	}
	@RequestMapping("/usersUpdateSubmit")
	public String usersUpdateSubmit(HttpServletRequest request)
	{
		String email=request.getParameter("email");
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("email", email);
		
		AUsers users=this.usersService.findOneUsersService(condMap);
		String operType=request.getParameter("operType");
		boolean updateFlag=false;
		if("updateInfo".equalsIgnoreCase(operType))
		{
			updateFlag=true;
			String qq=request.getParameter("qq");
			String address=request.getParameter("address");
			String regionId=request.getParameter("regionId");
		
			users.setAddress(address);
			users.setQq(qq);
			users.setRegionId(Integer.valueOf(regionId));
			
		}else if("updatePass".equalsIgnoreCase(operType))
		{
			String password=request.getParameter("password");
			String password1=request.getParameter("password1");
			
			if(EncryptUtil.checkStr(users.getPassword(), password))
			{
				updateFlag=true;
				users.setPassword(EncryptUtil.encodeStr(password1));
				
			}else{
				this.info="原密码不正确";
			}
			
			
		}else{
			this.info="违法操作";
		}
		
		if(updateFlag)
		{
			users.setUpdateTime(new Date());
			JSONObject resDbJSON=this.usersService.updateOneUsersService(users);
			this.info=resDbJSON.getString("info");
		}
		request.setAttribute("info", info);
		return this.usersUpdate(request);
	}
	
	
	
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpSession session)
	{
		
		session.removeAttribute("users");
		session.removeAttribute("lastLoginTime");
		
		String token=request.getParameter("token");
		if(token==null)
		{
			token="";
		}
		
		if(!"".equalsIgnoreCase(token))
		{
			ConstatFinalUtil.TOKENS_MAP.remove(token);
		}
		this.info=ConstatFinalUtil.INFO_JSON.getString("8");
		
		session.setAttribute("info", info);
		return "redirect:/login.htm";
	}
	
}
